package com.liveinstars.im.domain.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liveinstars.api.im.query.ReportQuery;
import com.liveinstars.im.domain.dao.model.Report;
import com.liveinstars.im.domain.manager.ReportManager;
import com.liveinstars.im.domain.dao.mapper.ReportMapper;
import com.onepiece.shipelves.common.constant.Constant;
import com.onepiece.shipelves.common.enums.SystemExceptionEnum;
import com.onepiece.shipelves.common.exception.DatabaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: zhouze
 * @date: 2020/3/3
 */
@Service("reportManager")
public class ReportManagerImpl extends ServiceImpl<ReportMapper, Report> implements ReportManager {

    private final static Logger logger = LoggerFactory.getLogger(ReportManagerImpl.class);

    @Autowired
    private ReportMapper reportMapper;

    @Override
    public void add(Report report) {
        try {
            this.save(report);
        } catch (Exception e) {
            logger.error("添加举报记录异常：{}", e.getMessage() , e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }

    @Override
    public Page<Report> pageByQuery(ReportQuery reportQuery) {
        try {
            Page<Report> page = new Page<>(reportQuery.getPageNum(), reportQuery.getPageSize());
            QueryWrapper<Report> queryWrapper = new QueryWrapper<>();
            if (reportQuery.getStatus() != null) {
                queryWrapper.eq("status", reportQuery.getStatus());
            }
            if (reportQuery.getType() != null) {
                queryWrapper.eq("type", reportQuery.getType());
            }
            queryWrapper.eq("state", Constant.STATE_VALID);
            queryWrapper.orderByDesc("cst_created");
            this.page(page, queryWrapper);
            return page;
        } catch (Exception e) {
            logger.error("获取举报记录分页列表异常：{}", e.getMessage() , e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }

    @Override
    public void updateStatusById(Long id, Integer status) {
        try {
            Report report = new Report();
            report.setId(id);
            report.setStatus(status);
            this.updateById(report);
        } catch (Exception e) {
            logger.error("更新举报记录处理状态异常：{}", e.getMessage() , e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }
}
